﻿#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable : 4996)

#include <iostream>
#include <Windows.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "BookSystem.h"
using namespace std;

//初始化书籍空链表
Status initBooksLinkList(BLinkList& books)
{
    books = new BNode;
    if (!books)
    {
        exit(OVERFLOW);
    }
    books->next = NULL;
    return OK;
}
/**
 *@brief 显示菜单的函数
 *
*/
void displayAdminMenu() {
    cout << "\n------------------------" << endl;
    cout << "(1) 显示所有书籍信息;\n(2) 添加书籍;\n(3) 查找指定书籍;\n(4) 删除指定书籍;\n(5) 修改指定书籍;\n(6) 退出系统;" << endl;
    cout << "------------------------" << endl;
}

//从文件中读入书籍信息
//books 图书链表头指针
//从文件中读取一条图书信息就存入链表中
int readBooksFromFile(BLinkList& books)
{
    FILE* file = fopen("D:\\MyGit\\giteeProject\\gitee-project\\gitee-project\\BookManagerSystem\\BookSystemProject\\book.txt", "r");
    BLinkList p = books, r;
    if (file == NULL)
    {
        printf("Error opening file!\n");
        return 0;
    }
    // read first line to count
    int count = 0;
    fscanf(file, "%d", &count);
    if (count == 0)
    {
        printf("no books\n");
        return 0;
    }
    for (int i = 0; i < count; i++)
    {
        r = new BNode;
        fscanf(file, "%s %s %s %f %d", r->data.ISBN, r->data.name,
            r->data.auth, &r->data.price, &r->data.inventory);
        r->next = NULL;
        p->next = r;
        p = r;
    }
    fclose(file);
    return count;
}

//把链表中的图书信息写入文件
void writeBooksToFile(BLinkList books, int count)
{
    FILE* file = fopen("D:\\MyGit\\giteeProject\\gitee-project\\gitee-project\\BookManagerSystem\\BookSystemProject\\book.txt", "w");
    BLinkList p = books->next;
    if (p == NULL)
    {
        printf("没有找到书籍信息，无法进行写入。");
        return;
    }
    if (file == NULL)
    {
        printf("Error opening file!\n");
        return;
    }
    fprintf(file, "%d\n", count);
    for (int i = 0; i < count; i++)
    {
        fprintf(file, "%s %s %s %.2f %d\n", p->data.ISBN, p->data.name,
            p->data.auth, p->data.price, p->data.inventory);
        p = p->next;
    }
    fclose(file);
}

int main()
{
    BLinkList books;
    BLinkList book;
    BLinkList insert_book;
    BLinkList pos = NULL;
    initBooksLinkList(insert_book);
    initBooksLinkList(book);
    initBooksLinkList(books);
    char choice;
    while (1)
    {
        int bookCount = readBooksFromFile(books);
        system("cls");
        printf("欢迎来到图书管理系统!\n");
        displayAdminMenu();
        printf("请输入你的选择(1 - 6): ");
        cin >> choice;
        switch (choice)
        {
        case '1':
            system("cls");
            displayAllBooks(books);
            printf("请按任意键继续...");
            _getch();
            break;
        case '2':
            system("cls");
            do {
                cout << "请输入书籍的ISBN: ";
                cin >> insert_book->data.ISBN;
                if (ISBN_islegal(insert_book->data.ISBN)) {
                    if (searchISBN(books, insert_book) != NULL)
                    {
                        cout << "该ISBN已经存在, 请重新输入ISBN" << endl;
                    }else {
                        break;
                    }
                }else{
                    cout << "输入错误，请重新输入ISBN" << endl;
                }
            } while (1);

            do {
                cout << "请输入书籍的书名: ";
                cin >> insert_book->data.name;
                if (chickValidity(insert_book->data.name, MAX_NAME)) {
                    if (searchNAME(books, insert_book)) {
                        cout << "相同书名不能有两种ISBN编号,请重新输入书名" << endl;
                    }else {
                        break;
                    }
                }else {
                    cout << "输入错误，请重新输入书名" << endl;
                }
            } while (1);

            do {
                cout << "请输入书籍的作者：";
                cin >> insert_book->data.auth;
                if (!(chickValidity(insert_book->data.auth, MAX_AUTHNAME))) {
                    cout << "输入错误，请重新输入作者" << endl;
                }
                else {
                    cout << "请输入书籍的价格：";
                    cin >> insert_book->data.price;
                    cout << "请输入书籍的库存：";
                    cin >> insert_book->data.inventory;
                    break;
                }
            } while (1);
            insertTheBooks(books,insert_book,bookCount);
            writeBooksToFile(books, bookCount);
            printf("请按任意键继续...");
            _getch();
            break;
        case '3':
            while (1)
            {
                system("cls");
                printf("欢迎来到书籍搜索界面!\n");
                searchMenu();
                printf("请输入你的选择(1 - 4): ");
                cin >> choice;
                switch (choice)
                {
                case '1':
                    system("cls");
                    while (1)
                    {
                        printf("请输入需要查找书的ISBN: ");
                        cin >> book->data.ISBN;
                        if (ISBN_islegal(book->data.ISBN))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("ISBN长度13-MAX_ISBN(15)且全为0-9数字\n");
                        }
                    }
                    if (searchISBN(books, book)) {
                        cout << "以下是这本书的相关信息" << endl;
                        cout << "ISBN：" << book->data.ISBN << endl;
                        cout << "书名：" << book->data.name << endl;
                        cout << "作者：" << book->data.auth << endl;
                        cout << "价格：" << book->data.price << endl;
                        cout << "库存：" << book->data.inventory << endl;
                    }
                    else {
                        cout << "没有找到这本书" << endl;
                    }
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '2':
                    system("cls");
                    while (1)
                    {
                        printf("请输入需要查找的作者名: ");
                        cin >> book->data.auth;
                        if (chickValidity(book->data.auth, MAX_AUTHNAME))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("作者名长度需满足0-MAX_AUTHNAME(11)，且全为a-z小写字母\n");
                        }
                    }
                    if (searchAUTH(books, book))
                    {
                        cout << "以下是关于这个作者的全部书籍信息" << endl;
                        BLinkList pe = book;
                        while (pe)
                        {
                            cout << "以下是这本书的相关信息" << endl;
                            cout << "ISBN：" << pe->data.ISBN << endl;
                            cout << "书名：" << pe->data.name << endl;
                            cout << "作者：" << pe->data.auth << endl;
                            cout << "价格：" << pe->data.price << endl;
                            cout << "库存：" << pe->data.inventory << endl;
                            pe = pe->next;
                        }
                    }
                    else {
                        cout << "没有找到关于这个作者的任何书籍" << endl;
                    }
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '3':
                    system("cls");
                    while (1)
                    {
                        printf("请输入需要查找的书名: ");
                        cin >> book->data.name;
                        if (chickValidity(book->data.name, MAX_NAME))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("书名长度需满足0-MAX_NAME(51)，且全为a-z小写字母\n");
                        }
                    }
                    if (searchNAME(books, book))
                    {
                        cout << "以下是这本书的相关信息" << endl;
                        cout << "ISBN：" << book->data.ISBN << endl;
                        cout << "书名：" << book->data.name << endl;
                        cout << "作者：" << book->data.auth << endl;
                        cout << "价格：" << book->data.price << endl;
                        cout << "库存：" << book->data.inventory << endl;
                    }
                    else {
                        cout << "没有找到这本书" << endl;
                    }
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '4':
                    system("cls");
                    printf("退出搜索界面...\n");
                    goto exitSearch;
                default:
                    system("cls");
                    printf("非法输入!\n");
                    printf("请按任意键继续...");
                    _getch();
                    break;
                }
            }exitSearch:
            printf("请按任意键继续...");
            _getch();
            break;
        case '4':
            while (1)
            {
                system("cls");
                printf("欢迎来到书籍删除界面!\n");
                deleteMenu();
                printf("请输入你的选择(1 - 4): ");
                cin >> choice;
                switch (choice)
                {
                case '1':
                    system("cls");
                    while (1)
                    {
                        printf("请输入需要删除书的ISBN: ");
                        cin >> book->data.ISBN;
                        if (ISBN_islegal(book->data.ISBN))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("ISBN长度13-MAX_ISBN(15)且全为0-9数字\n");
                        }
                    }
                    pos = searchPriorISBN(books, book);
                    if (pos != NULL) {
                        deleteTheBooks(pos, bookCount);
                    }
                    else {
                        printf("未找到匹配的书籍。\n");
                    }
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '2':
                    system("cls");
                    while (1)
                    {
                        printf("请输入需要删除书的作者名: ");
                        cin >> book->data.auth;
                        if (chickValidity(book->data.auth, MAX_AUTHNAME))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("作者名长度需满足0-MAX_AUTHNAME(11)，且全为a-z小写字母\n");
                        }
                    }
                    pos = searchPriorAUTH(books, book);
                    if (pos != NULL) {
                        deleteTheBooks(pos, bookCount);
                    }
                    else {
                        printf("未找到匹配的书籍。\n");
                    }
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '3':
                    system("cls");
                    while (1)
                    {
                        printf("请输入需要删除书的的书名: ");
                        cin >> book->data.name;
                        if (chickValidity(book->data.name, MAX_NAME))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("书名长度需满足0-MAX_NAME(51)，且全为a-z小写字母\n");
                        }
                    }
                    pos = searchPriorNAME(books, book);
                    if (pos != NULL) {
                        deleteTheBooks(pos, bookCount);
                    }
                    else {
                        printf("未找到匹配的书籍。\n");
                    }
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '4':
                    system("cls");
                    printf("退出删除界面...\n");
                    goto exitDelete;
                default:
                    system("cls");
                    printf("非法输入!\n");
                    printf("请按任意键继续...");
                    _getch();
                    break;
                }
            }exitDelete:
            writeBooksToFile(books, bookCount);
            printf("请按任意键继续...");
            _getch();
            break;
        case '5':
            while (1)
            {
                system("cls");
                modifyBysearchMenu();
                printf("请输入你的选择(1 - 3): ");
                cin >> choice;
                switch (choice)
                {
                case '1':
                    system("cls");
                    while (1)
                    {
                        printf("请输入需要删除书的ISBN: ");
                        cin >> book->data.ISBN;
                        if (ISBN_islegal(book->data.ISBN))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("ISBN长度13-MAX_ISBN(15)且全为0-9数字\n");
                        }
                    }
                    pos = searchISBN(books, book);
                    if (pos != NULL) {
                        goto out;
                    }
                    else {
                        printf("未找到匹配的书籍。\n");
                    }
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '2':
                    system("cls");
                    while (1)
                    {
                        printf("请输入需要删除书的作者名: ");
                        cin >> book->data.auth;
                        if (chickValidity(book->data.auth, MAX_AUTHNAME))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("作者名长度需满足0-MAX_AUTHNAME(11)，且全为a-z小写字母\n");
                        }
                    }
                    if (searchAUTH(books, book)) {
                        displayBook(book);
                        while (1)
                        {
                            printf("请输入需要删除关于这个作者的书的ISBN: ");
                            cin >> book->data.ISBN;
                            if (ISBN_islegal(book->data.ISBN))
                            {
                                break;
                            }
                            else {
                                printf("非法输入!\n");
                                printf("ISBN长度13-MAX_ISBN(15)且全为0-9数字\n");
                            }
                        }
                        pos = searchISBN(books, book);
                        if (pos != NULL) {
                            goto out;
                        }
                        else {
                            printf("未找到匹配的书籍。\n");
                        }
                    }
                    else {
                        printf("未找到匹配的书籍。\n");
                    }
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '3':
                    system("cls");
                    while (1)
                    {
                        printf("请输入需要删除书的的书名: ");
                        cin >> book->data.name;
                        if (chickValidity(book->data.name, MAX_NAME))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("书名长度需满足0-MAX_NAME(51)，且全为a-z小写字母\n");
                        }
                    }
                    pos = searchNAME(books, book);
                    if (pos != NULL) {
                        goto out;
                    }
                    else {
                        printf("未找到匹配的书籍。\n");
                    }
                    printf("请按任意键继续...");
                    _getch();
                    break;
                default:
                    system("cls");
                    printf("非法输入!\n");
                    printf("请按任意键继续...");
                    _getch();
                    break;
                }
            }out:
            while (1)
            {
                system("cls");
                printf("欢迎来到书籍修改界面!\n");
                modifyMenu();
                printf("请输入你的选择(1 - 6): ");
                cin >> choice;
                switch (choice)
                {
                case '1':
                    system("cls");
                    char newISBN[MAX_ISBN];
                    while (1)
                    {
                        printf("请输入修改后的ISBN: ");
                        cin >> newISBN;
                        if (ISBN_islegal(newISBN))
                        {
                            strcpy(book->data.ISBN, newISBN);
                            if (searchISBN(books, book) != NULL) {
                                printf("该ISBN已经存在,请重新修改...\n");
                            }
                            else {
                                break;
                            }
                        }
                        else {
                            printf("非法输入!\n");
                            printf("ISBN长度13-MAX_ISBN(15)且全为0-9数字\n");
                        }
                    }
                    modifyISBN(pos, newISBN);
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '2':
                    system("cls");
                    char newAuth[MAX_AUTHNAME+1];
                    while (1)
                    {
                        printf("请输入需要删除书的作者名: ");
                        cin >> newAuth;
                        if (chickValidity(newAuth, MAX_AUTHNAME))
                        {
                            break;
                        }
                        else {
                            printf("非法输入!\n");
                            printf("作者名长度需满足0-MAX_AUTHNAME(11)，且全为a-z小写字母\n");
                        }
                    }
                    modifyAuth(pos, newAuth);
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '3':
                    system("cls");
                    char newName[MAX_NAME+1];
                    while (1)
                    {
                        printf("请输入修改后的书名: ");
                        cin >> newName;
                        if (chickValidity(newName, MAX_NAME))
                        {
                            strcpy(book->data.name, newName);
                            if (searchNAME(books, book) != NULL) {
                                printf("该书名已经存在,请重新修改...\n");
                            }
                            else {
                                break;
                            }
                        }
                        else {
                            printf("非法输入!\n");
                            printf("书名长度需满足0-MAX_NAME(51)，且全为a-z小写字母\n");
                        }
                    }
                    modifyName(pos, newName);
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '4':
                    system("cls");
                    float newPrice;
                    printf("请输入修改后的价格: ");
                    cin >> newPrice;
                    modifyPrice(pos, newPrice);
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '5':
                    system("cls");
                    int newInventory;
                    printf("请输入修改后的库存: ");
                    cin >> newInventory;
                    modifyInventory(pos, newInventory);
                    printf("请按任意键继续...");
                    _getch();
                    break;
                case '6':
                    system("cls");
                    printf("退出修改界面...\n");
                    goto exitModify;
                default:
                    system("cls");
                    printf("非法输入!\n");
                    printf("请按任意键继续...");
                    _getch();
                    break;
                }
            }exitModify:
            writeBooksToFile(books, bookCount);
            printf("请按任意键继续...");
            _getch();
            break;
        case '6':
            system("cls");
            printf("退出图书管理系统...\n");
            return 0;
        default:
            system("cls");
            printf("非法输入!\n");
            printf("请按任意键继续...");
            _getch();
            break;
        }
    }
}



